﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using GhostStory.Model;
using Microsoft.Xna.Framework.Media;
namespace GhostStory.View
{
    class View : Model.IObserver
    {
        private SpriteBatch m_spriteBatch;
        private SpriteBatch m_SpiritBackGround;
        private Content m_content;

        
        private float m_viewScale = 100;
        
        private float m_time = 0;
        
        private int m_textureTileSize = 32;
        private SmokeSystem m_smoke = new SmokeSystem();
        private SnowSystem m_snow = new SnowSystem();
        bool songstart = false;

        public View(GraphicsDeviceManager a_manager, Content a_content)
        {
            m_content = a_content;

            m_SpiritBackGround = new SpriteBatch(a_manager.GraphicsDevice);
            m_spriteBatch = new SpriteBatch(a_manager.GraphicsDevice);

        }



        private void DrawPlayerAt(Microsoft.Xna.Framework.Vector2 a_viewBottomCenterPosition, float a_scale)
        {

            Rectangle destRect = new Rectangle((int)(a_viewBottomCenterPosition.X - a_scale / 2.0f), (int)(a_viewBottomCenterPosition.Y - a_scale), (int)a_scale, (int)a_scale);
            m_spriteBatch.Draw(m_content.m_playerTexture, destRect, Color.Red);
            
        }

        internal void DrawEnemyAt(GraphicsDevice a_graphicsDevice, Camera a_camera, Vector2 enemyPosition)
        {
            float scale = a_camera.GetScale();
            Vector2 viewportSize = new Vector2(a_graphicsDevice.Viewport.Width, a_graphicsDevice.Viewport.Height);
            Vector2 playerViewPos = a_camera.GetViewPosition(enemyPosition.X, enemyPosition.Y, viewportSize);
            
            m_spriteBatch.Begin();
            Rectangle destRect = new Rectangle((int)(playerViewPos.X - scale / 2.0f), (int)(playerViewPos.Y - scale), (int)scale, (int)scale);
            m_spriteBatch.Draw(m_content.m_enemyTexture, destRect, Color.Gray);
            m_spriteBatch.End();
            
        }
        

        private void DrawUI(int points)
        {
            
            Vector2 pos = new Vector2(20, 20);
            m_spriteBatch.DrawString(m_content.m_font, "Points: " + Convert.ToString(points), pos, Color.Red);
        }

        internal void DrawMenu(GraphicsDevice a_graphicsDevice, Camera a_camera, Vector2 a_playerPosition, int points, Color color)
        {
            m_spriteBatch.Begin();
            Vector2 viewportSize = new Vector2(a_graphicsDevice.Viewport.Width, a_graphicsDevice.Viewport.Height);
            Vector2 displacement = new Vector2(500, 250);
            m_spriteBatch.DrawString(m_content.m_font, "GAME OVER! ", displacement, color);
            displacement = new Vector2(500, 280);
            m_spriteBatch.DrawString(m_content.m_font, "You got " + Convert.ToString(points) + " points", displacement, color);
            displacement = new Vector2(500, 310);
            m_spriteBatch.DrawString(m_content.m_font, "Press Enter to RESTART!", displacement, color);
            m_spriteBatch.End();
            
        }

        internal void DrawStart(GraphicsDevice a_graphicsDevice, Camera a_camera, Vector2 a_playerPosition, int points)
        {
            m_spriteBatch.Begin();
            Vector2 viewportSize = new Vector2(a_graphicsDevice.Viewport.Width, a_graphicsDevice.Viewport.Height);
            Vector2 displacement = new Vector2(500, 250);
            m_spriteBatch.DrawString(m_content.m_font, "WELCOME TO GHOSTSTORY!", displacement, Color.Yellow);
            displacement = new Vector2(500, 280);
            m_spriteBatch.DrawString(m_content.m_font, "Press ENTER to start", displacement, Color.Yellow);
            m_spriteBatch.End();
            

        }

        internal void DrawClearLevel1(GraphicsDevice a_graphicsDevice, Camera a_camera, Vector2 a_playerPosition, int points)
        {
            m_spriteBatch.Begin();
            Vector2 viewportSize = new Vector2(a_graphicsDevice.Viewport.Width, a_graphicsDevice.Viewport.Height);
            Vector2 displacement = new Vector2(450, 250);
            m_spriteBatch.DrawString(m_content.m_font, "CONGRATULATIONS! LEVEL 1 CLEAR!", displacement, Color.Yellow);
            displacement = new Vector2(450, 280);
            m_spriteBatch.DrawString(m_content.m_font, "You got " + Convert.ToString(points) + " points", displacement, Color.Yellow);
            displacement = new Vector2(450, 310);
            m_spriteBatch.DrawString(m_content.m_font, "Press R to RESTART", displacement, Color.Yellow);
            displacement = new Vector2(450, 340);
            m_spriteBatch.DrawString(m_content.m_font, "Or press ENTER to continue to the DESERT", displacement, Color.Yellow);
            m_spriteBatch.End();


        }

        internal void DrawClearLevel2(GraphicsDevice a_graphicsDevice, Camera a_camera, Vector2 a_playerPosition, int points)
        {
            m_spriteBatch.Begin();
            Vector2 viewportSize = new Vector2(a_graphicsDevice.Viewport.Width, a_graphicsDevice.Viewport.Height);
            Vector2 displacement = new Vector2(450, 250);
            m_spriteBatch.DrawString(m_content.m_font, "CONGRATULATIONS! LEVEL 2 CLEAR!", displacement, Color.Black);
            displacement = new Vector2(450, 280);
            m_spriteBatch.DrawString(m_content.m_font, "You got " + Convert.ToString(points) + " points", displacement, Color.Black);
            displacement = new Vector2(450, 310);
            m_spriteBatch.DrawString(m_content.m_font, "Press R to RESTART", displacement, Color.Black);
            displacement = new Vector2(450, 340);
            m_spriteBatch.DrawString(m_content.m_font, "Or press ENTER to continue to the MOUNTAINS", displacement, Color.Black);
            m_spriteBatch.End();


        }

        internal void DrawClearLevel3(GraphicsDevice a_graphicsDevice, Camera a_camera, Vector2 a_playerPosition, int points)
        {
            m_spriteBatch.Begin();
            Vector2 viewportSize = new Vector2(a_graphicsDevice.Viewport.Width, a_graphicsDevice.Viewport.Height);
            Vector2 displacement = new Vector2(350, 250);
            m_spriteBatch.DrawString(m_content.m_font, "CONGRATULATIONS! COMPLETED THE GAME (More levels coming soon)!", displacement, Color.Black);
            displacement = new Vector2(450, 280);
            m_spriteBatch.DrawString(m_content.m_font, "You got " + Convert.ToString(points) + " points", displacement, Color.Black);
            displacement = new Vector2(450, 310);
            m_spriteBatch.DrawString(m_content.m_font, "Press R to RESTART", displacement, Color.Black);
            
            m_spriteBatch.End();


        }

        private void DrawTile(float a_x, float a_y, float a_scale, Model.Level.Tile a_tile)
        {

            //Get the source rectangle (pixels on the texture) for the tile type 
            Rectangle sourceRectangle = new Rectangle(m_textureTileSize * 1, 0, m_textureTileSize, m_textureTileSize);

            //Destination rectangle in windows coordinates only scaling
            Rectangle destRect = new Rectangle((int)a_x, (int)a_y, (int)a_scale, (int)a_scale);

            if(a_tile == Model.Level.Tile.T_BLOCKED)
                m_spriteBatch.Draw(m_content.m_tileTexture, destRect, Color.White);
            if(a_tile == Model.Level.Tile.T_EGG)
                m_spriteBatch.Draw(m_content.m_coinTexture, destRect, Color.White);
            if (a_tile == Model.Level.Tile.T_BLACKHOLE)
                m_spriteBatch.Draw(m_content.m_tileTextureBlackHole, destRect, Color.White);
        }

        public void DrawLevel(GraphicsDevice a_graphicsDevice, Model.Level a_level, Camera a_camera, Vector2 a_playerPosition, int a_playerPoints, float a_elapsedTime, Controller.MasterController.TestState a_state)
        {
            Vector2 viewportSize = new Vector2(a_graphicsDevice.Viewport.Width, a_graphicsDevice.Viewport.Height);
            float scale = a_camera.GetScale();

            a_graphicsDevice.Clear(Microsoft.Xna.Framework.Color.White);

            m_SpiritBackGround.Begin();
            m_SpiritBackGround.Draw(m_content.m_backGroundTexture, Vector2.Zero, null,
                Color.White);
            m_SpiritBackGround.End();

            //draw all images
            m_spriteBatch.Begin();

            m_time += a_elapsedTime / 10.0f;

            Vector2 displacement = a_camera.GetViewPosition(a_playerPosition.X - 1.5f, a_playerPosition.Y - 0.5f, viewportSize);

            m_smoke.DrawSmoke(m_time * 4.0f, displacement + new Vector2(150.0f, 0), m_viewScale, m_spriteBatch, m_content.m_smokeTexture);

            

            //draw level
            for (int x = 0; x < Model.Level.g_levelWidth; x++)
            {
                for (int y = 0; y < Model.Level.g_levelHeight; y++)
                {
                    Vector2 viewPos = a_camera.GetViewPosition(x, y, viewportSize);
                    DrawTile(viewPos.X, viewPos.Y, scale, a_level.m_tiles[x, y]);
                }
            }

            //draw player on top of level
            Vector2 playerViewPos = a_camera.GetViewPosition(a_playerPosition.X, a_playerPosition.Y, viewportSize);
            if(a_state == Controller.MasterController.TestState.StatePlaying)
                DrawPlayerAt(playerViewPos, scale);

            if (a_level.getCurrentLevel() == Model.Level.CurrentLevel.LEVEL_3)
                m_snow.Draw(m_spriteBatch, m_content.m_snowTexture, a_elapsedTime);

            DrawUI(a_playerPoints);

            
            m_spriteBatch.End();
        }


        public void playBgMusic()
        {

            if (!songstart)
            {
                MediaPlayer.Play(m_content.m_bgMusic);
                MediaPlayer.Volume = 0.5f;
                songstart = true;
            } 
        }




        
    }
}
